package com.youshang.rocketlearning;

import com.alibaba.fastjson.JSON;
import com.youshang.kafkalearning.model.User;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;
import org.joda.time.DateTime;

import java.io.Serializable;
import java.time.LocalDateTime;

/**
* @Author 18011618
* @Date 10:41 2018/7/17
* @Function 消息生产者
*/
public class LocalRocketMQProducer {
    public static void main(String[] args) throws MQClientException {
        DefaultMQProducer producer = new DefaultMQProducer("test-group");
        producer.setNamesrvAddr("192.168.3.123:9876");
        producer.setInstanceName("rmq-instance");
        producer.start();
        try {
            for (int i = 0; i < 5; i++) {
                User user = new User();
                user.setLoginName("abc" + i + "   "+ DateTime.now());
                user.setPwd(String.valueOf(i));
                Message message = new Message("log-topic", "user-tag", JSON.toJSONString(user).getBytes());
                System.out.println("生产者发送消息:" + JSON.toJSONString(user));
                producer.send(message);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        producer.shutdown();
    }

}